package com.ffyc.resale.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ffyc.resale.model.Buy;
import com.ffyc.resale.model.Goods;
import com.ffyc.resale.model.Result;
import com.ffyc.resale.service.BuyService;
import com.ffyc.resale.service.GoodsService;
import com.ffyc.resale.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/buy")
public class BuyController {
    @Autowired
    BuyService buyService;
    @PostMapping("/list")
    public Result list(@RequestHeader String adminToken, @RequestBody Buy buy){
        IPage<Buy> list = buyService.list(buy);
        return new Result(200,"查询成功",list);
    }
    @PostMapping("/search")
    public Result search(@RequestHeader String adminToken, @RequestBody Buy buy){
        IPage<Buy> list = buyService.search(buy);
        return new Result(200,"查询成功",list);
    }
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        buyService.deleteById(id);
        return new Result(200,"删除成功",null);
    }
    /**
     * 购买记录
     * */
    @PostMapping("/getOrderList")
    public Result getOrderList(@RequestHeader String userToken, @RequestBody Buy buy){
        IPage<Buy> list = buyService.getOrderList(userToken,buy);
        return new Result(200,"查询成功",list);
    }
    /**支付*/
    @PostMapping("/pay/{orderId}")
    public Result pay(@RequestHeader String userToken,@PathVariable Integer orderId){
        buyService.updateStatus(orderId,1);
        return new Result(200,"支付成功",null);
    }
    /**收货*/
    @PostMapping("/confirmReceive/{orderId}")
    public Result confirmReceive(@RequestHeader String userToken,@PathVariable Integer orderId){
        buyService.updateStatus(orderId,3);
        return new Result(200,"收货成功",null);
    }
    /**取消订单*/
    @PostMapping("/cancel/{orderId}")
    public Result cancel(@RequestHeader String userToken,@PathVariable Integer orderId){
        buyService.updateStatus(orderId,4);
        return new Result(200,"订单已取消",null);
    }
    /**订单*/
    @PostMapping("/getSaleList")
    public Result getSaleList(@RequestHeader String userToken, @RequestBody Buy buy){
        IPage<Buy> list = buyService.getSaleList(userToken,buy);
        return new Result(200,"查询成功",list);
    }
    /** 卖家发货 */
    @PostMapping("/delivery/{id}")
    public Result delivery(@RequestHeader String userToken,@PathVariable Integer id){
        buyService.updateStatus(id,2);
        return new Result(200,"货物已发出",null);
    }
    @PostMapping("/buyGoods")
    public Result buyGoods(@RequestHeader String userToken,@RequestBody Buy buy ){
        int id = JWTUtil.id(userToken);
        buy.setUserbuy(id);
        buyService.buyGoods(buy);
        return new Result(200,"成功",null);
    }
}
